using System;
using System.Collections.Generic;
using System.Text;
using Sedge.Core.Configuration;
using Sedge.Core.FileSystemWrap;
using Sedge.Core.Globalization;

namespace Sedge.Core.Execution.ReportElements
{
	public class ReportFileList : ReportElementBase
	{
		private readonly List<string> _fileNames = new List<string>();
		private readonly List<int> _files = new List<int>();

		public bool ExtendedMode { get; set; }

		public ReportFileList(Suite suite, bool extendedMode)
			: base(suite)
		{
			ExtendedMode = extendedMode;
		}

		public override bool CanMergeWith(IReportElement element)
		{
			bool canMerge = base.CanMergeWith(element);
			if (canMerge)
			{
				ReportFileList fileList = element as ReportFileList;
				if (fileList == null)
					canMerge = false;
				else
					canMerge = fileList.ExtendedMode == ExtendedMode;
			}

			return canMerge;
		}

		public override void MergeWith(IReportElement element)
		{
			ReportFileList reportFileList = element as ReportFileList;
			if (reportFileList != null)
			{
				if (ExtendedMode)
					_fileNames.AddRange(reportFileList._fileNames);
				else
					_files.AddRange(reportFileList._files);
			}
		}

		public void AddFile(string filePath, FileType fileType)
		{
			if (ExtendedMode)
				_fileNames.Add(filePath);
			else
				_files.Add(Suite.FilesRegistry.Register(filePath, fileType));
		}

		public void AddFiles(IEnumerable<string> files, FileType fileType)
		{
			foreach (string file in files)
			{
				AddFile(file, fileType);
			}
		}

		public override string GetHtml()
		{
			StringBuilder sb = new StringBuilder(GetHeaderAsHtml());
			sb.Append("<table class=\"file-list\">");
			sb.AppendFormat("<tr><th>{0}</th><th>{1}</th>",
								Locale.TemplateStrings.FileListNamesCol,
								Locale.TemplateStrings.FileListOriginCol);

			if (ExtendedMode)
			{
				sb.AppendFormat("<th>{0}</th><th>{1}</th>",
								Locale.TemplateStrings.FileListSizeCol,
								Locale.TemplateStrings.FileListVersionCol);
			}

			sb.Append("</tr>");
			
			if (ExtendedMode)
			{
				foreach (string fileName in _fileNames)
				{
					ReportFile(sb, fileName, fileName);
				}
			}
			else
			{
				foreach (int regID in _files)
				{
					RegisteredFileInfo fileInfo = Suite.FilesRegistry.GetFile(regID);
					ReportFile(sb, fileInfo.OriginalPath, fileInfo.NewPath);
				}
			}

			sb.Append("</table>");

			return sb.ToString();
		}

		private void ReportFile(StringBuilder sb, string originalPath, string newPath)
		{
			string fileName = Services.FileSystem.Path.GetFileName(originalPath);
			string pattern = ExtendedMode ? "<tr><td>{1}</td><td>{2}</td>" : "<tr><td><a href=\"{0}\">{1}</a></td><td>{2}</td>";

			sb.AppendFormat(pattern, newPath, fileName, originalPath);

			if (ExtendedMode)
			{
				IFileInfoWrap fileInfoWrap = Services.FileSystem.File.GetFileInfo(originalPath);
				sb.AppendFormat("<td>{0}</td><td>{1}</td>",
								Convert.ToString(fileInfoWrap.FileSize),
								Convert.ToString(fileInfoWrap.FileVersion));
			}

			sb.Append("</tr>");
		}
	}
}